package com.bt.att;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: zakir
 * Date: 9/5/11
 * Time: 7:51 PM
 * To change this template use File | Settings | File Templates.
 */
public class BillParserOld {
    private static final Logger logger = LoggerFactory.getLogger(BillParserOld.class);

    void parse(File file, RecordHandler handler) {
		StringBuffer sb = new StringBuffer();
		FileInputStream fstream = null;
		BufferedReader br = null;
        Map<String, String> record = new HashMap<String, String>();
		try {
			fstream = new FileInputStream(file);
			br = new BufferedReader(new InputStreamReader(fstream));
			String line;
            String person = "";
            boolean recordStarted = false;
            Integer counter = 0;
            int ii = 0;
			while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.equals("<tr class=\"rowclr\">")) {
                    record.clear();
                    record.put("person", person);
                    recordStarted = true;
                    counter = 0;
                    continue;
                }
                String tag = "<h3>&nbsp;User Name :&nbsp;";
                ii = line.indexOf(tag);
                if (ii > 0) {
                    line = line.substring(ii + tag.length());
                    ii = line.indexOf(";");
                    line = line.substring(ii+1);
                    person = line.replace("</h3>", "").replace("</td>", "").trim();
                }

                if (recordStarted && line.trim().startsWith("<td ") && !line.contains("width")) {
                    ii = line.indexOf(">");
                    line = line.substring(ii+1);
                    line = line.substring(0, line.indexOf("</td>")).trim();
                    line = line.replaceAll("<nobr>", "");
                    line = line.replaceAll("</nobr>", "");
                    switch(counter++) {
                        case 0: record.put("item", line); break;
                        case 1: record.put("day", line); break;
                        case 2: record.put("date", line); break;
                        case 3: record.put("time", line); break;
                        case 4: /*record.put("n1", line);*/ break;
                        case 5: record.put("number", line); break;
                        case 6: record.put("city", line); break;
                        case 7: record.put("duration", line); break;
                        case 8: record.put("rateCode", line); break;
                        case 9: record.put("ratePd", line); break;
                        case 10: record.put("feature", line); break;
                        case 11: record.put("charge", line); break;
                        case 12: record.put("addCharge", line); break;
                        case 13: record.put("totalCharge", line); break;
                    }
                }

                if (recordStarted && line.trim().startsWith("</tr>")) {
                    handler.handle(record);
                    recordStarted = false;
                }
				sb.append(line);
			}
			br.close();
			fstream.close();
		} catch (Exception e) {
			logger.error("Error: " + e.getMessage());
		} finally {
			try {
				if (br != null){
					br.close();
				}
				if (fstream != null){
					fstream.close();
				}
			} catch (Exception ee) {
				logger.error("Error in closing the stream: "
						+ ee.getMessage());
			}
		}
    }
}
